/*
 * Created Jul 7, 2010
 */
package ltg.ps.phenomena.zooroom;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

import ltg.ps.api.phenomena.Phenomena;
import ltg.ps.api.phenomena.PhenomenaWindow;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * TODO Description
 *
 * @author Gugo
 */
public class Trace1Win extends PhenomenaWindow {
	
	private List<Record> data = null;
	private UpdateMode updateMode;
	

	public Trace1Win(String windowName) {
		super(windowName);
		// TODO Auto-generated constructor stub
	}

	
	/* (non-Javadoc)
	 * @see ltg.ps.model.PhenomenaWindow#update(ltg.ps.model.Phenomena)
	 */
	@Override
	public void update(Phenomena p) {
		if(updateMode==UpdateMode.STATE) {
			data = ((ZooRoom)p).getPlayerCurrentState(1).getData();
		} else {
			data = ((ZooRoom)p).getPlayerUpdates(1);
		}
		//if (data!= null && !data.isEmpty())
			//net.sendXML("zooRoomTest",toXML());
			//System.out.println("ciao");
	}

	
	@Override
	public String toXML() {
		
		Document d = DocumentHelper.createDocument();
		d.addElement("phenUpdate")
		.addAttribute("type", "state")
		.addAttribute("phen", "zooroom")
		.addElement("player")
		.addAttribute("id", "1");
		//Add all data
		Record r = null;
		for(Iterator<Record>i=data.iterator();i.hasNext();) {
			r = i.next();
			d.addElement("record")
			.addAttribute("long", Float.toString(r.getLon()))
			.addAttribute("lat", Float.toString(r.getLat()))
			.addAttribute("time", Integer.toString(r.getTime()));
		}
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setSuppressDeclaration(true);
		XMLWriter xmlw = null;
		try {
			xmlw = new XMLWriter(format);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		try {
			xmlw.write(d);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return d.asXML(); 
	}

}
